package threadPoll;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

public class ThreadPoolExecutorDome1 {


    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor executor = init();
        for (int i = 1; i < 2000; i++) {
            Integer integer = i;
            executor.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + ": i=" + integer+"【"+"当前线程池中的线程数："+executor.getPoolSize()+"】");
            });
        }
        /**
         * 线程池使用完毕之后记得关闭，否则新创的线程会一直挂起。导致内存溢出现象
         */
        executor.shutdown();
    }


    /**
     * int corePoolSize：核心线程数
     * int maximumPoolSize：最大线程数
     * long keepAliveTime：空闲线程存活的时间
     * TimeUnit unit：空闲线程存活的时间单位
     * BlockingQueue<Runnable> workQueue：阻塞队列
     * @return
     */
    public static ThreadPoolExecutor init(){
        ThreadPoolExecutor executor=new ThreadPoolExecutor(
                10, 200, 30, TimeUnit.SECONDS,new LinkedBlockingQueue<>()
        );
        return executor;
    }


}
